package util;

import java.sql.*;

public class DBUtil {
    //数据库petstore的连接与关闭操作,可修改_C
    private static final String driverStr = "com.mysql.cj.jdbc.Driver";
    private static final String connectStr = "jdbc:mysql://localhost:3306/jpetstore?serverTimezone=UTC&characterEncoding=utf8";
    private static final String username = "root";
    private static final String password = "123456";

    public static Connection getConnection() throws Exception {
        Connection connection;
        Class.forName(driverStr);
        connection = DriverManager.getConnection(connectStr,username,password);
        return connection;
    }

    public static void closeStatement(Statement statement) throws Exception {
        statement.close();
    }

    public static void closePreparedStatement(PreparedStatement preparedStatement) throws Exception {
        preparedStatement.close();
    }

    public static void closeResultSet(ResultSet resultSet) throws Exception {
        resultSet.close();
    }

    public static void closeConnection(Connection connection) throws Exception {
        connection.close();
    }

    //测试用例
//    public static void main(String[] args) throws Exception{
//        Connection connection = DBUtil.getConnection();
//        System.out.println(connection);
//    }
}
